#include "stdafx.h"
#include "flow_tracker.h"
#include "title.h"

using namespace std;

FlowTracker::FlowTracker(Common* _common, SceneView* _scene, automaticItem* _parent, const bool display_title)
	: common(_common), scene(_scene), parent(_parent)
{
	init_model();
	vector<QString> list_of_script = scene->common->getScriptList();
	foreach(QString script_name, list_of_script)
		addScriptParameter(script_name);
	connect(common, SIGNAL(layer_is_added(const double &)), this, SLOT(addLayer(const double &)));
}

FlowTracker::~FlowTracker(){
}


void FlowTracker::init_model(){
	ParametersModel *model = parent->getModel();
	QString common_parameter = "COMMON";
	model->addItem(common_parameter, "LOCATION//.X", parent->pos().x(), "px", 1, 0, "X position");
	model->addItem(common_parameter, "LOCATION//.Y", parent->pos().y(), "px", 1, 0, "Y position");
	//...........
}


void FlowTracker::addScriptParameter(const QString &script_name){	
	
}

void FlowTracker::removeScriptParameter(const QString &script_name){

}

void FlowTracker::popupShow(QGraphicsSceneContextMenuEvent *event){
	QMenu menu;	
	QAction* actIndicator = new QAction("coming soon...:)", this);
	menu.addAction(actIndicator);;
	menu.exec(event->screenPos());
}

void FlowTracker::addLayer( const double &time ){
	vector<autConPoint*> autConPointList = parent->getListOfAutConPoint();
	for(int index = 0; index < autConPointList.size(); ++index){
		autConPoint* point = autConPointList[index];
		automaticItem *sensor = point->getOtherItem();
		if (sensor != NULL){
			if (sensor->isSensor()){
				QString sensor_keyword = index < 2 ? "FLOW" : "PRESSURE";
				string s_sensor_keyword = sensor_keyword.toStdString();
				wstring w_sensor_keyword = wstring(s_sensor_keyword.begin(), s_sensor_keyword.end());

				QString keyword = QString("%1 : %2").arg(sensor->getName()).arg(sensor_keyword);
				string s_keyword = keyword.toStdString();
				wstring w_keyword = wstring(s_keyword.begin(), s_keyword.end());
				double value = sensor->getData()[s_sensor_keyword].back();
				parent->addDataLayer(keyword, value);
				}else{
					QMessageBox::critical(0, "Error", "Flow Tracker: incorrect connection!", QMessageBox::Ok);	
					break; break;
				}
			}	
		}
}
